
Imports std

Public Class argmgr

    Public Const argprefix As String = character.minusSign + character.leftSlash

    Public Shared Sub parse(ByVal args() As String, ByRef values As map(Of String, vector(Of String)),
                            Optional ByVal lcase As Boolean = False,
                            Optional ByVal clear As Boolean = True)
        If values Is Nothing Then
            values = New map(Of String, vector(Of String))()
        ElseIf clear Then
            values.clear()
        End If

        Dim name As String = emptyString
        Dim i As Int32
        For i = 0 To args.Length() - 1
            If strlen(args(i)) > 0 AndAlso argprefix.IndexOf(args(i)(0)) <> npos Then
                name = strmid(args(i), 1)
                If lcase Then
                    name = name.ToLower()
                End If
                If values.find(name) = values.end() Then
                    values.insert(name, New vector(Of String)())
                End If
            Else
                values(name).push_back(args(i))
            End If
        Next
    End Sub

    Public Shared Function contains(ByVal args As map(Of String, vector(Of String)) _
                                    , ByVal argument As String, Optional ByVal lcase As Boolean = False) As Boolean
        If lcase Then
            Return (args.find(argument.ToLower) <> args.end())
        Else
            Return (args.find(argument) <> args.end())
        End If
    End Function

    Public Shared Function containsValue(ByVal args As map(Of String, vector(Of String)) _
                                         , ByVal argument As String, Optional ByVal lcase As Boolean = False) As Boolean
        If lcase Then
            Return (contains(args, argument, lcase) AndAlso args(argument.ToLower).size > 0)
        Else
            Return (contains(args, argument, lcase) AndAlso args(argument).size > 0)
        End If
    End Function

    Public Shared Function getfirstvalue(ByVal args As map(Of String, vector(Of String)) _
                                        , ByVal argument As String, ByRef value As String _
                                        , Optional ByVal lcase As Boolean = False) As Boolean
        Dim work As map(Of String, vector(Of String)).iterator = Nothing
        If lcase Then
            work = args.find(argument.ToLower)
        Else
            work = args.find(argument)
        End If
        If work = args.end OrElse (+work).second.size = 0 Then
            value = Nothing
            Return False
        Else
            copy(Of String)(value, (+work).second(0))
            Return True
        End If
    End Function

    Public Shared Function firstvalue(ByVal args As map(Of String, vector(Of String)) _
                                      , ByVal argument As String, Optional ByVal lcase As Boolean = False) As String
        Dim rtn As String = Nothing
        If getfirstvalue(args, argument, rtn, lcase) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function getfirstvalue(Of t)(ByVal args As map(Of String, vector(Of String)) _
                                                , ByVal argument As String, ByRef value As t _
                                                , Optional ByVal lcase As Boolean = False) As Boolean
        Dim valueStr As String = Nothing
        If getfirstvalue(args, argument, valueStr, lcase) Then
            value = convertor.cast(Of t)(valueStr, False)
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function firstvalue(Of t)(ByVal args As map(Of String, vector(Of String)) _
                                            , ByVal argument As String, Optional ByVal lcase As Boolean = False) As t
        Dim rtn As t = Nothing
        If getfirstvalue(args, argument, rtn, lcase) Then
            Return rtn
        Else
            Return Nothing
        End If
    End Function

End Class
